<!--通知栏 选中方法名按住ctr键接口-->
<template>
  <div>
    <div class="search">
      <el-form :model="form01" size="small" :inline="true">
        <div style="display: flex; align-items: center;">
          <el-form-item label="性别" prop="type">
            <el-select v-model="form01.sex" placeholder="请选择性别" clearable style="width: 200px">
              <el-option
                  v-for="item in sexList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="姓名" prop="name" >
            <el-input placeholder="请输入姓名" style="width: 200px" v-model="form01.name" clearable></el-input>
          </el-form-item>
          <el-form-item label="部门名称" prop="deptId">
            <el-select  v-model="form01.deptId" placeholder="请输选择部门名称" clearable style="width: 200px">
              <el-option
              v-for="item in deptList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="info" plain @click="load(1)">查询</el-button>
            <el-button type="warning" plain @click="reset">重置</el-button>
          </el-form-item>
        </div>
        </el-form>
    </div>

    <div class="operation">
      <el-button type="primary" plain @click="handleAdd">新增</el-button>
      <el-button type="danger" plain @click="delBatch">批量删除</el-button>
    </div>

    <div class="table">
      <el-table :data="tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="序号" width="80" align="center" sortable></el-table-column>
        <el-table-column prop="name" label="员工姓名" show-overflow-tooltip></el-table-column>
        <el-table-column prop="position" label="职位" show-overflow-tooltip></el-table-column>
        <el-table-column prop="hireDate" label="入职日期" show-overflow-tooltip></el-table-column>
        <el-table-column prop="deptName" label="部门名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="sex" label="性别">
          <template slot-scope="scope">
            <span v-if="scope.row.sex === 1">男</span>
            <span v-if="scope.row.sex === 2">女</span>
            <span v-if="scope.row.sex === 3">未知</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template v-slot="scope">
            <el-button plain type="primary" @click="handleEdit(scope.row)" size="mini">编辑</el-button>
            <el-button plain type="danger" size="mini" @click=del(scope.row.id)>删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </div>

    <!--对话框-->
    <el-dialog :title="title" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
      <el-form label-width="100px" style="padding-right: 50px" :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="name" label="员工姓名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="position" label="职位">
          <el-input v-model="form.position" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item prop="sex" label="性别">
         <el-select v-model="form.sex" placeholder="请选择性别">
           <el-option
               v-for="item in sexList"
               :key="item.value"
               :label="item.label"
               :value="item.value">
           </el-option>
         </el-select>
        </el-form-item>

        <el-form-item prop="deptId" label="部门名称">
          <el-select v-model="form.deptId"placeholder="请选择部门">
            <el-option
                v-for="item in deptList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
export default {
  name: "Employee",
  data() {
    return {
      title: null,
      tableData: [],  // 所有的数据
      deptList: [], // 部门的所有数据
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      fromVisible: false,
      form: {},
      form01: {
        sex: '',
        name: '',
        deptId: '',
      },
      sexList: [
        {label: "男", value: 1},
        {label: "女", value: 2},
        {label: "未知", value: 3},
      ],
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      // 进行校验
      rules: {
        type: [
          {required: true, message: '请输入部门类型', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '请输入部门名称', trigger: 'blur'},
        ]
      },
      ids: []
    }
  },
  created() {
    this.load(1)
    this.load01()
  },
  methods: {
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.fromVisible = true   // 打开弹窗
      this.title = "添加员工"
    },
    handleEdit(row) {   // 编辑数据 传行数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = true   // 打开弹窗
      this.title = "修改员工"
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {  // 进行表单校验
        if (valid) { // 如果为true则认证通过
          this.$request({
            // 如果有ID则是更新操作，否则为添加操作
            url: this.form.id ? '/employee/update' : '/employee/add',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/employee/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    // 选中数据(行)
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      // 给ids复制
      this.ids = rows.map(v => v.id)   //  [1,2]
    },
    delBatch() {   // 批量删除
      if (!this.ids.length) { // 没有选择的话则会提示你选择数据
        this.$message.warning('请选择数据')
        return
      }
      // 做判断
      this.$confirm('您确定批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/employee/delete/batch', {data: this.ids}).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1) // 进行查询
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/employee/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          sex: this.form01.sex,
          name: this.form01.name,
          deptId: this.form01.deptId,
        }
      }).then(res => {
        this.tableData = res.data?.list
        this.total = res.data?.total
      })
    },
    load01() {  //
       this.$request.get('/dept/selectAll').then(res => {
        this.deptList = res?.data
        console.log("hello world",this.deptList)
      })
    },

    reset() {
      this.form01.sex = null,
      this.form01.name = null,
      this.form01.deptId = null,
          this.load(1)
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
  }
}
</script>

<style scoped>

</style>
